package com.ruoyi.workSafetyStandardization.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.workSafetyStandardization.domain.InspectionItemResult;
import com.ruoyi.workSafetyStandardization.domain.InspectionTeamMembers;
import com.ruoyi.workSafetyStandardization.dto.inspection.MajorDto;
import com.ruoyi.workSafetyStandardization.service.IInspectionTeamMembersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workSafetyStandardization.mapper.InspectionMajorResultMapper;
import com.ruoyi.workSafetyStandardization.domain.InspectionMajorResult;
import com.ruoyi.workSafetyStandardization.service.IInspectionMajorResultService;

/**
 * 标准化考核专业评分Service业务层处理
 *
 * @author pengjie
 * @date 2023-12-18
 */
@Service
public class InspectionMajorResultServiceImpl implements IInspectionMajorResultService
{
    @Autowired
    private InspectionMajorResultMapper inspectionMajorResultMapper;
    @Autowired
    private IInspectionTeamMembersService teamMembersService;

    /**
     * 查询标准化考核专业评分
     *
     * @param majorResultId 标准化考核专业评分主键
     * @return 标准化考核专业评分
     */
    @Override
    public InspectionMajorResult selectInspectionMajorResultByMajorResultId(String majorResultId)
    {
        return inspectionMajorResultMapper.selectInspectionMajorResultByMajorResultId(majorResultId);
    }

    /**
     * 查询标准化考核专业评分列表
     *
     * @param inspectionMajorResult 标准化考核专业评分
     * @return 标准化考核专业评分
     */
    @Override
    public List<InspectionMajorResult> selectInspectionMajorResultList(InspectionMajorResult inspectionMajorResult)
    {
        return inspectionMajorResultMapper.selectInspectionMajorResultList(inspectionMajorResult);
    }

    /**
     * 新增标准化考核专业评分
     *
     * @param inspectionMajorResult 标准化考核专业评分
     * @return 结果
     */
    @Override
    public int insertInspectionMajorResult(InspectionMajorResult inspectionMajorResult)
    {
        inspectionMajorResult.setCreateByName(SecurityUtils.getUsername());
        inspectionMajorResult.setCreateAt(new Date());
        inspectionMajorResult.setCreateBy(SecurityUtils.getUserId().toString());
        return inspectionMajorResultMapper.insertInspectionMajorResult(inspectionMajorResult);
    }

    /**
     * 修改标准化考核专业评分
     *
     * @param inspectionMajorResult 标准化考核专业评分
     * @return 结果
     */
    @Override
    public int updateInspectionMajorResult(InspectionMajorResult inspectionMajorResult)
    {
        return inspectionMajorResultMapper.updateInspectionMajorResult(inspectionMajorResult);
    }

    /**
     * 批量删除标准化考核专业评分
     *
     * @param majorResultIds 需要删除的标准化考核专业评分主键
     * @return 结果
     */
    @Override
    public int deleteInspectionMajorResultByMajorResultIds(String[] majorResultIds)
    {
        return inspectionMajorResultMapper.deleteInspectionMajorResultByMajorResultIds(majorResultIds);
    }

    /**
     * 删除标准化考核专业评分信息
     *
     * @param majorResultId 标准化考核专业评分主键
     * @return 结果
     */
    @Override
    public int deleteInspectionMajorResultByMajorResultId(String majorResultId)
    {
        return inspectionMajorResultMapper.deleteInspectionMajorResultByMajorResultId(majorResultId);
    }

    /**
     * 通过inspectionId查询majorList
     * @param inspectionId
     * @return
     */
    @Override
    public List<InspectionMajorResult> queryMajorListByInspectionId(String inspectionId) {
        return inspectionMajorResultMapper.queryMajorListByInspectionId( inspectionId) ;
    }


    /**
     * 通过majorId查询子数据
     * @param parentId 父id
     * @return
     */
    @Override
    public List<InspectionMajorResult> queryChildrenByParentId(String parentId){

        return inspectionMajorResultMapper.queryChildrenByParentId(parentId);
    }

    @Override
    public List<MajorDto> getChildrenTree(String majorId, List<InspectionMajorResult> majorResultList) {
        //List<MajorDto> majorDtoList=new ArrayList<>();
        //BeanUtils.copyProperties(majorDtoList,majorDtoList);
        List<MajorDto> list=majorResultList.stream().filter(at->at.getParentId().equals(majorId)
        ).map(it->{
            MajorDto majorDto=new MajorDto();
            BeanUtils.copyProperties(it,majorDto);
            majorDto.setChildren(getChildrenTree(it.getMajorId(),majorResultList));
            List<InspectionTeamMembers> leaderList=teamMembersService.queryLeaderListByInspectionIdAndMajorId(majorDto);
            majorDto.setMajorLeaderList(leaderList);
            return majorDto;
                }
        ).collect(Collectors.toList());

        return list;
    }
}
